import * as THREE from 'three'

/**
 * 初始化场景
 * @param {*} bgColor 背景颜色
*/
function initScene(bgColor = 0x000000) {
    const scene = new THREE.Scene()
    scene.background = new THREE.Color(bgColor)
    return scene
}


/**
 * 初始化相机
 * @param {*} config
 * fov: 视角
 * width: 宽度
 * height: 高度
 * near: 近端
 * far: 远端
 * position: 位置
*/
function initCamera(config = {}) {
    const { fov = 75, width = window.innerWidth, height = window.innerHeight, near = 0.1, far = 1000, position = [0, 0, 5] } = config
    const camera = new THREE.PerspectiveCamera(fov, width / height, near, far);
    camera.position.set(...position);
    return camera;
}


/**
 * 初始化渲染器
 * @param {*} config
 * width: 宽度
 * height: 高度
*/
function initRenderer(config = {}) {
    const { width = window.innerWidth, height = window.innerHeight } = config
    const renderer = new THREE.WebGLRenderer({ antialias: true })
    renderer.setPixelRatio(window.devicePixelRatio) //设置像素比以支持高分辨率屏幕
    renderer.setSize(width, height)

    // 监听窗口大小变化事件并调整渲染器大小
    // window.addEventListener('resize', () => {
    //     renderer.setSize(width, height);
    // });
    return renderer;
}


/**
 * 初始化环境光
 * @param {*} config
 * lightColor: 光颜色
 * intensity: 光强度
 * position: 光位置 
 */
function initAmbientLight(config = {}) {
    const { lightColor = 0xffffff, intensity = 1, position= [0, 0, 0] } = config
    const light = new THREE.AmbientLight(lightColor, intensity)
    light.position.set(...position)
    return light

}


/**
 * 初始化坐标轴
 * @param {*} config
 * size: 轴的线段长度
  */
function initAxesHelper({size = 1}) {
    const axesHelper = new THREE.AxesHelper(size)
    return axesHelper
}



export {
    initScene,
    initCamera,
    initRenderer,
    initAmbientLight,
    initAxesHelper
}